<template>
  <t-dialog
    header="提示"
    placement="center"
    :visible.sync="visible"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
  >
    <div style="height: 100px">
      <div style="margin-bottom: 10px">请输入"{{ userName }}"的新密码</div>
      <t-form ref="form" :colon="true"  :data="formData"  labelWidth="0" :rules="rules">
        <t-form-item label="密码" name="password">
          <t-input clearable v-model="formData.password"  placeholder="请输入新的密码"></t-input>
        </t-form-item>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
import {
  resetUserPwd
} from "@/api/system/user";
import { encrypt } from "@/utils/jsencrypt";

export default {
  name: "reset-user-password",
  props:["userId","userName"],
  data(){
    return {
      visible: true,
      loading: false,
      formData: {
        password: "",
      },
      rules:{
        password: [
          {
            required: true,
            message: "密码不能为空",
          },
          {
            pattern: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9~!@#$%^&*]{8,20}/,
            message: '请输入 8-20 个字符，需同时包含数字、大小写字母以及特殊符号',
            type: 'error',
          },
        ],
      }
    }
  },
  methods:{
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          resetUserPwd(this.userId,encrypt(this.formData.password)).then((response) => {
            this.$message.success(`修改成功，新密码是：${this.formData.password}`);
            // this.$emit("reload");
            this.$emit("update:visible", false);
            this.loading = false;
          }).catch((e) => {
            this.loading = false;
            this.$message.error(e.toString());
          });
        }})
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
  }
}
</script>

<style scoped>

</style>
